<?php


namespace api\modules\app\services;

use Yii;
use api\components\helpers\Constant;
use yii\helpers\ArrayHelper;

class LogService
{
    //新增
    const SUB_TYPE_CREATE = 2;
    //修改
    const SUB_TYPE_UPDATE = 3;
    //删除
    const SUB_TYPE_DELETE = 4;
    //导入
    const SUB_TYPE_IMPORT = 5;
    //导出
    const SUB_TYPE_EXPORT = 6;

    private $errMsg;

    public function getErrMsg() {
        return $this->errMsg;
    }

    public $createOpLogUrl = 'log/create-op-log';

    public function logToUserCenter($data)
    {
        if (!isset($data['user_id']) || !isset($data['module_name']) || !isset($data['sub_type'])) {
            $this->errMsg = '缺少参数';
            return false;
        }

        try {
            $userCenterHost = defined('USER_CENTER_HOST_2') ? USER_CENTER_HOST_2 : 'http://12.113.232.230:8081/api/';

            //module_name,user_id,sub_type,remark
            $url = $userCenterHost . $this->createOpLogUrl;
            $timestamp = time();
            $sign = $this->generateSign(USER_CENTER_APPID, USER_CENTER_APPKEY, $timestamp);
            $params = [
                'appId' => USER_CENTER_APPID,
                'timestamp' => $timestamp,
                'sign' => $sign
            ];
            $url = $url . '?' . http_build_query($params);

            $data['app_id'] = USER_CENTER_APPID;
            $response = $this->request($url, 'POST', $data);
            $response = json_decode($response, true);

            if (ArrayHelper::getValue($response, 'error', 1) === 1) {
                $code = ArrayHelper::getValue($response, 'data.status');
                $message = ArrayHelper::getValue($response, 'data.message', '');
                $result = ['code' => $code, 'message' => $message, 'status' => Constant::API_STATUS_FAILED];
                Yii::$app->response->data = $result;
                Yii::$app->response->send();
                Yii::$app->end();
            }
            return $response['data'];
        } catch(\Exception $e) {

        }
    }

    public function generateSign($appId, $appKey, $timestamp)
    {
        $signStr = "appId=%s&timestamp=%s&secret=%s";
        $signStr = sprintf($signStr, $appId, $timestamp, $appKey);
        return sha1($signStr);
    }

    public function request(string $url, string $method = "get", $params = [])
    {
        $method = strtolower($method);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if ($method == 'post') {
            curl_setopt($curl, CURLOPT_POST, 1); //post提交方式
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        }
        $response = curl_exec($curl);
        return $response;
    }

    //用户中心3.0 user
    public function logByUser($user, $data) {
        if (empty($user)) {
            return false;
        }
        $data['user_id'] = $user['id'];
        $data['username'] = $user['username'];
        $data['name'] = $user['name'];
        $data['phone'] = $user['phone'];

        $this->logToUserCenter($data);
    }

}
